package baibeiwallet.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.alm.token.R;
import com.alm.token.base.BaseViewPageFragment;
import com.alm.token.ui.persenter.MyPreenter;

import org.greenrobot.eventbus.EventBus;
import org.web3j.crypto.CipherException;

import java.io.IOException;

import baibeiwallet.BaibeiWallet;
import baibeiwallet.WalletManager;
import baibeiwallet.event.CreateWalletEvent;
import baibeiwallet.event.SelWalletEvent;
import baibeiwallet.utils.BaiBeiWalletUtils;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class InportFrag extends BaseViewPageFragment {

    @BindView(R.id.et_data_inprot)
    public EditText mEtData;
    @BindView(R.id.et_pwd)
    public EditText mEtPwd;
    @BindView(R.id.et_pwd1)
    public EditText mEtPwd1;
    @BindView(R.id.et_name)
    public EditText mEtName;
    @BindView(R.id.btn_body)
    public LinearLayout btn_body;


    private int type;

    public static InportFrag newInstance(int Type) {
        InportFrag fragment = new InportFrag();
        Bundle args = new Bundle();
        args.putInt("Type", Type);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected MyPreenter initPreenter() {
        return null;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_inprot_wallet3;
    }

    @Override
    protected void initVariables() {
        type = getArguments().getInt("Type");
//        showToast("" + type);
        for (int i = 0; i < btn_body.getChildCount(); i++) {
            btn_body.getChildAt(i).setVisibility(View.GONE);
        }
        btn_body.getChildAt(type).setVisibility(View.VISIBLE);
    }

    @Override
    protected void initViews(View view) {

        if (type == 0) {
            mEtData.setHint(getString(R.string.wallet_import_hint2));
        } else if (type == 1) {
            mEtData.setHint(getString(R.string.wallet_import_hint3));
            mEtPwd.setHint(getString(R.string.wallet_import_hint4));
        } else if (type == 2) {
            mEtData.setHint(getString(R.string.wallet_import_hint5));
        } else
            mEtData.setHint(getString(R.string.wallet_import_hint1));
    }

    @Override
    public void onClick(View v) {

    }


    @Override
    protected void loadData() {

    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @OnClick(R.id.btn_inprot_mnemonic)
    public void onClickInportMnemonic() {
        String pwd = mEtPwd.getText().toString();
        String pwd1 = mEtPwd1.getText().toString();
        String mnemonic = mEtData.getText().toString();

        if (!checkPwd(pwd, pwd1)) {
            return;
        }

        String name = mEtName.getText().toString();
        if (TextUtils.isEmpty(name)) {
            showToast(getString(R.string.wallet_import_hint7));
//            Toast.makeText(this, "钱包名不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        if (TextUtils.isEmpty(mnemonic)) {
            showToast(getString(R.string.wallet_import_hint12));
//            Toast.makeText(this, "助记词不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        try {
            BaibeiWallet baibeiWallet = BaiBeiWalletUtils.generateBip44Wallet(mnemonic, pwd);
            baibeiWallet.setPassword(pwd);
            baibeiWallet.setWalletName(name);
            if (WalletManager.getInstance().addWallet(baibeiWallet)) {
                showToast(getString(R.string.wallet_import_hint8));
                EventBus.getDefault().post(new CreateWalletEvent());
                EventBus.getDefault().post(new SelWalletEvent(0));
                Intent i = new Intent(getActivity(), WalletDetailActivity1.class);
                i.putExtra("index", 0);
                startActivity(i);
                getActivity().finish();
            } else
                showToast(getString(R.string.wallet_import_hint9));
//            WalletManager.getInstance().addWallet(baibeiWallet);
//            showToast("导入成功");
//            EventBus.getDefault().post(new CreateWalletEvent());
//
//            Intent i = new Intent(getActivity(), WalletDetailActivity1.class);
//            i.putExtra("index", 0);
//            startActivity(i);
//            getActivity().finish();

        } catch (CipherException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    @OnClick(R.id.btn_inprot_keystore)
    public void onClickInportKeystore() {
        String pwd = mEtPwd.getText().toString();
        String pwd1 = mEtPwd1.getText().toString();
        String keystore = mEtData.getText().toString();
        if (!checkPwd(pwd, pwd1)) {
            return;
        }
        if (TextUtils.isEmpty(keystore)) {
            showToast(getString(R.string.wallet_import_hint10));
//            Toast.makeText(this, "Keystore不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        String name = mEtName.getText().toString();
        if (TextUtils.isEmpty(name)) {
            showToast(getString(R.string.wallet_import_hint7));
//            Toast.makeText(this, "钱包名不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        try {
            BaibeiWallet baibeiWallet = BaiBeiWalletUtils.loadByKeyStore(pwd, keystore);
            baibeiWallet.setPassword(pwd);
            baibeiWallet.setWalletName(name);
            if (WalletManager.getInstance().addWallet(baibeiWallet)) {
                showToast(getString(R.string.wallet_import_hint8));
                EventBus.getDefault().post(new CreateWalletEvent());
                EventBus.getDefault().post(new SelWalletEvent(0));
                Intent i = new Intent(getActivity(), WalletDetailActivity1.class);
                i.putExtra("index", 0);
                startActivity(i);
                getActivity().finish();

            } else
                showToast(getString(R.string.wallet_import_hint9));
//            WalletManager.getInstance().addWallet(baibeiWallet);
//            showToast("导入成功");
//            EventBus.getDefault().post(new CreateWalletEvent());
//
//            Intent i = new Intent(getActivity(), WalletDetailActivity1.class);
//            i.putExtra("index", 0);
//            startActivity(i);
//            getActivity().finish();
        } catch (CipherException e) {
            e.printStackTrace();
            if (e.toString().indexOf("Invalid password provided") != -1) {
                showToast(getString(R.string.wallet_import_hint11));
            }
        } catch (IOException e) {
            e.printStackTrace();
            if (e.toString().indexOf("Invalid password provided") != -1) {
                showToast(getString(R.string.wallet_import_hint11));
            }
        }
    }

    @OnClick(R.id.btn_inprot_key)
    public void onClickInportKey() {
        String pwd = mEtPwd.getText().toString();
        String pwd1 = mEtPwd1.getText().toString();
        String privateKey = mEtData.getText().toString();
        if (!checkPwd(pwd, pwd1)) {
            return;
        }
        if (TextUtils.isEmpty(privateKey)) {
            showToast(getString(R.string.wallet_import_hint6));
//            Toast.makeText(this, "私钥不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        String name = mEtName.getText().toString();
        if (TextUtils.isEmpty(name)) {
            showToast(getString(R.string.wallet_import_hint7));
//            Toast.makeText(this, "钱包名不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        try {
            BaibeiWallet baibeiWallet = BaiBeiWalletUtils.generateBip44WalletByPrivateKey(privateKey, pwd);
            baibeiWallet.setPassword(pwd);
            baibeiWallet.setWalletName(name);
            if (WalletManager.getInstance().addWallet(baibeiWallet)) {
                showToast(getString(R.string.wallet_import_hint8));
                EventBus.getDefault().post(new CreateWalletEvent());
                EventBus.getDefault().post(new SelWalletEvent(0));
                Intent i = new Intent(getActivity(), WalletDetailActivity1.class);
                i.putExtra("index", 0);
                startActivity(i);
                getActivity().finish();

            } else
                showToast(getString(R.string.wallet_import_hint9));
//            WalletManager.getInstance().addWallet(baibeiWallet);
//            showToast("导入成功");
//            EventBus.getDefault().post(new CreateWalletEvent());
//
//            Intent i = new Intent(getActivity(), WalletDetailActivity1.class);
//            i.putExtra("index", 0);
//            startActivity(i);
//            getActivity().finish();
        } catch (CipherException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    private boolean checkPwd(String pwd, String pwd1) {
        if (TextUtils.isEmpty(pwd)) {
            showToast(getString(R.string.wallet_hint_pleaseinputpsw));
//            Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show();
            return false;
        }
        if (TextUtils.isEmpty(pwd1)) {
            showToast(getString(R.string.wallet_hint_pleaseinputpsw1));
//            Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show();
            return false;
        }
        if (!pwd1.equalsIgnoreCase(pwd)) {
            showToast(getString(R.string.profile_hint_warn13));
//            Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }


}
